package com.tttare.haoyigou.sellers.service.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tttare.haoyigou.mapper.TbBrandMapper;
import com.tttare.haoyigou.mapper.TbGoodsDescMapper;
import com.tttare.haoyigou.mapper.TbGoodsMapper;
import com.tttare.haoyigou.mapper.TbItemCatMapper;
import com.tttare.haoyigou.mapper.TbItemMapper;
import com.tttare.haoyigou.mapper.TbSellerMapper;
import com.tttare.haoyigou.pojo.TbBrand;
import com.tttare.haoyigou.pojo.TbGoods;
import com.tttare.haoyigou.pojo.TbGoodsDesc;
import com.tttare.haoyigou.pojo.TbGoodsExample;
import com.tttare.haoyigou.pojo.TbGoodsExample.Criteria;
import com.tttare.haoyigou.pojo.TbItem;
import com.tttare.haoyigou.pojo.TbItemCat;
import com.tttare.haoyigou.pojo.TbSeller;
import com.tttare.haoyigou.pojogroup.Goods;
import com.tttare.haoyigou.sellers.service.GoodsService;

import entity.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {


	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbGoods> findAll() {
		return goodsMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbGoods> page=   (Page<TbGoods>) goodsMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(TbGoods goods){
		goodsMapper.updateByPrimaryKey(goods);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbGoods findOne(Long id){
		return goodsMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			goodsMapper.deleteByPrimaryKey(id);
		}		
	}
	
	
		@Override
	public PageResult findPage(TbGoods goods, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbGoodsExample example=new TbGoodsExample();
		Criteria criteria = example.createCriteria();
		
		if(goods!=null){			
						if(goods.getSellerId()!=null && goods.getSellerId().length()>0){
				criteria.andSellerIdLike("%"+goods.getSellerId()+"%");
			}
			if(goods.getGoodsName()!=null && goods.getGoodsName().length()>0){
				criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
			}
			if(goods.getAuditStatus()!=null && goods.getAuditStatus().length()>0){
				criteria.andAuditStatusLike("%"+goods.getAuditStatus()+"%");
			}
			if(goods.getIsMarketable()!=null && goods.getIsMarketable().length()>0){
				criteria.andIsMarketableLike("%"+goods.getIsMarketable()+"%");
			}
			if(goods.getCaption()!=null && goods.getCaption().length()>0){
				criteria.andCaptionLike("%"+goods.getCaption()+"%");
			}
			if(goods.getSmallPic()!=null && goods.getSmallPic().length()>0){
				criteria.andSmallPicLike("%"+goods.getSmallPic()+"%");
			}
			if(goods.getIsEnableSpec()!=null && goods.getIsEnableSpec().length()>0){
				criteria.andIsEnableSpecLike("%"+goods.getIsEnableSpec()+"%");
			}
			if(goods.getIsDelete()!=null && goods.getIsDelete().length()>0){
				criteria.andIsDeleteLike("%"+goods.getIsDelete()+"%");
			}
	
		}
		
		Page<TbGoods> page= (Page<TbGoods>)goodsMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	@Autowired
	private TbGoodsMapper goodsMapper;
		
	
	@Autowired
	private TbGoodsDescMapper tbGoodsDescMapper;
	
	@Autowired
	private TbItemCatMapper tbItemCatMapper;
	
	@Autowired
	private TbItemMapper tbItemMapper;
	
	@Autowired
	private TbSellerMapper tbSellerMapper;
	
	@Autowired
	private TbBrandMapper tbBrandMapper;

	//商品添加涉及三张表的操作,商品spu表,商品描述,商品sku
	@Override
	public void add(Goods goods) {
		//获取tbGoods
		TbGoods tbgoods = goods.getGoods();
		//设置商品的状态
		tbgoods.setAuditStatus("0");//未审核
		goodsMapper.insert(tbgoods);//插入并获取id
		//设置商品描述
		TbGoodsDesc goodsDesc = goods.getGoodsDesc();
		goodsDesc.setGoodsId(tbgoods.getId());
		tbGoodsDescMapper.insert(goodsDesc);//保存商品描述
		//判断此商品是否启用规格
		if(tbgoods.getIsEnableSpec().equals("1")){
			//启用规格
			for(TbItem item:goods.getItemList()){
				insertItem(tbgoods, goodsDesc, item);
				String title=tbgoods.getGoodsName();
				Map<String,Object> specMap = JSON.parseObject(item.getSpec(),Map.class);
				StringBuilder sb = new StringBuilder();
				for(String key:specMap.keySet()){
					sb.append(specMap.get(key)+" ");
				}
				title=title+" "+sb.toString();
				//设置标题
				item.setTitle(title);
				//保存item
				tbItemMapper.insert(item);
			}
		}else{
			//不启用规格
			TbItem item = new TbItem();
			insertItem(tbgoods,goodsDesc,item);
			item.setTitle(tbgoods.getGoodsName());
			item.setPrice(tbgoods.getPrice());
			item.setNum(9999);
			item.setStatus("1");//状态
			item.setIsDefault("1");//默认
			item.setSpec("{}");//规格			
			//保存item
			tbItemMapper.insert(item);
		}
		
	}

	private void insertItem(TbGoods tbgoods, TbGoodsDesc goodsDesc, TbItem item) {
		//设置item
		item.setGoodsId(tbgoods.getId());
		item.setCategoryid(tbgoods.getCategory3Id());//目录
		TbItemCat itemCat = tbItemCatMapper.selectByPrimaryKey(tbgoods.getCategory3Id());
		item.setCategory(itemCat.getName());
		//设置图片
		List<Map> list = JSON.parseArray(goodsDesc.getItemImages(),Map.class);
		if(list.size()>0){
			String imgUrl = (String) list.get(0).get("url");
			item.setImage(imgUrl);
		}
		//创建时间
		item.setCreateTime(new Date());
		item.setUpdateTime(new Date());
		//商家id
		item.setSellerId(tbgoods.getSellerId());
		//品牌名称
		TbBrand brand = tbBrandMapper.selectByPrimaryKey(tbgoods.getBrandId());
		item.setBrand(brand.getName());
		//店铺名称
		TbSeller seller = tbSellerMapper.selectByPrimaryKey(tbgoods.getSellerId());
		item.setSeller(seller.getNickName());
	}
	
}
